#include "bdd.h"

byte
td_del_row (unsigned int si, char *id, byte mode)
{
  session *stmp;
  row *rtmp;
  stmp = get_session (si);
  td_err = 0;
  if (!stmp)
    {
      td_err = INVALID_SESSION;
      return 0;
    }

  if (!stmp->subptr)
    {
      td_err = INVALID_SUBBASE;
      return 0;
    }

  if (mode == SELECTED)
    {
      if (!stmp->rowptr)
	{
	  td_err = INVALID_ROW;
	  return 0;
	}
      stmp->subptr->rows = del_row (stmp->subptr, stmp->rowptr);
      clean_session_row (stmp->rowptr);
      if (stmp->rootptr->mode == O_AUTO)
	write_bdd (stmp->baseptr);
      return 1;
    }

  if (!valid_string (id))
    {
      td_err = INVALID_STRING;
      return 0;
    } 
    
    
  rtmp = get_row (stmp->subptr->rows, id, stmp->rootptr->case_flag);
  if (!rtmp)
    {
      td_err = INVALID_ROW;
      return 0;
    }

  stmp->subptr->rows = del_row (stmp->subptr, rtmp);
  clean_session_row (rtmp);
  if (stmp->rootptr->mode == O_AUTO)
    write_bdd (stmp->baseptr);
  return 1;
}

byte
td_del_subbase (unsigned int si, char *id, byte mode)
{
  session *stmp;
  subbase *subtmp;
  stmp = get_session (si);
  td_err = 0;
  if (!stmp)
    {
      td_err = INVALID_SESSION;
      return 0;
    }

  if (!stmp->subptr)
    {
      td_err = INVALID_SUBBASE;
      return 0;
    }

  if (mode == SELECTED)
    {
      if (!stmp->subptr->parent)
	{
	  td_err = INVALID_SUBBASE;
	  return 0;
	}
      stmp->subptr->parent->enfant = del_subbase (stmp->subptr->parent->enfant, stmp->subptr);
      clean_session_subbase (stmp->subptr);
      if (stmp->rootptr->mode == O_AUTO)
	write_bdd (stmp->baseptr);
      return 1;
    }

  if (!valid_string (id))
    {
      td_err = INVALID_STRING;
      return 0;
    }
    
  subtmp = get_subbase (stmp->subptr->enfant, id, stmp->rootptr->case_flag);
  if (!subtmp)
    {
      td_err = INVALID_SUBBASE;
      return 0;
    }

  stmp->subptr->enfant = del_subbase (stmp->subptr->enfant, subtmp);
  clean_session_subbase (subtmp);
  if (stmp->rootptr->mode == O_AUTO)
    write_bdd (stmp->baseptr);
  return 1;
}


byte
td_del_subbase_p (unsigned int si, char *buffer, char delim)
{
  td_row *n = td_select_subbase_p (si, buffer, delim);
  if (!n)
    return 0;
  else
    td_free_row (n);
  return (td_del_subbase (si, buffer, SELECTED));
}


byte
td_del_base (unsigned int si, char *id, byte mode)
{
  session *stmp;
  racine *root_tmp;

  stmp = get_session (si);
  td_err = 0;
  if (!stmp)
    {
      td_err = INVALID_SESSION;
      return 0;
    }

  if (mode == SELECTED)
    {
      root_head = del_base (root_head, stmp->rootptr);
      clean_session_subbase (stmp->baseptr);
      clean_session_base (stmp->baseptr);
      return 1;
    }

  if (!valid_string (id))
    {
      td_err = INVALID_STRING;
      return 0;
    }
    
  root_tmp = get_base (root_head, id);
  if (!root_tmp)
    {
      td_err = INVALID_BASE;
      return 0;
    }

  root_head = del_base (root_head, root_tmp);
  clean_session_subbase (stmp->baseptr);
  clean_session_base (stmp->baseptr);
  return 1;
}
